package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/11/29.<br><br>
 *
 * 中等：解码方法
 */
public class Topic91 {

    public static void main(String[] args) {
        Topic91 instance = new Topic91();

        System.out.println(instance.numDecodings("12"));
        System.out.println(instance.numDecodings("226"));
        System.out.println(instance.numDecodings("0"));
        System.out.println(instance.numDecodings("06"));
    }

    public int numDecodings(String s) {
        if(s.startsWith("0")) {
            return 0;
        }

        int[] counter = new int[s.length()];
        counter[0] = 1;

        char[] array = s.toCharArray();
        for(int i = 1; i < s.length(); i++) {
            if(array[i] != '0') {
                counter[i] += counter[i - 1];
            }

            if(array[i - 1] != '0' && (array[i - 1] - '0') * 10 + array[i] - '0' <= 26) {
                counter[i] += i > 1 ? counter[i - 2] : 1;
            }
        }

        return counter[s.length() - 1];
    }
}
